package fr.jullebarge.canardroid;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class PostAdapter extends BaseAdapter {

	private Context mContext;
	private Post[] mPost;
	
	public PostAdapter(Context context, Post[] tabPost){
		mContext = context;
		mPost = tabPost;
	}
	
	public int getCount() {
		return mPost.length;
	}

	public Object getItem(int position) {
		return mPost[position];
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	 
		View rowView = inflater.inflate(R.layout.post_listview, parent, false);
		WebView avatar = (WebView) rowView.findViewById((R.id.avatar));
		TextView user = (TextView) rowView.findViewById((R.id.user));
		WebView html = (WebView) rowView.findViewById((R.id.html));
		
		TextView date = (TextView) rowView.findViewById((R.id.date));
		
		avatar.loadData(mPost[position].getPostAvatar(),"text/html", "UTF-8");
		user.setText(mPost[position].getPostUser());
		date.setText(mPost[position].getPostDate());
		html.loadData(mPost[position].getPostHtml(),"text/html", "UTF-8");
		
		avatar.setTag(position);
		user.setTag(position);
		html.setTag(position);
		date.setTag(position);
		
		/*topic.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Integer position = (Integer)v.getTag();
				sendListener(mTopic[position], position);
			}
		});*/
		
		return rowView;
	}

}
